<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\kdniao\addons;

use app\admin\model\Config;
/**
 * 使用快递鸟物流查询
 */
class Kdniao 
{
    /**
     * 物流查询
     * @param $ShipperCode string 公司编号
     * @param $LogisticCode string 快递单号
     */
    public static function select($ShipperCode, $LogisticCode){
        $config = Config::getVal('app_kdniao');
        if ($config) {
            // 组装应用级参数
            $requestData= "{".
            "'CustomerName': '',".
            "'OrderCode': '',".
            "'ShipperCode': '".$ShipperCode."',".
            "'LogisticCode': '".$LogisticCode."',".
            "}";
            // 组装系统级参数
            $datas = array(
                'EBusinessID' => $config['EBusinessID'],
                'RequestType' => '1002', //免费即时查询接口指令1002/在途监控即时查询接口指令8001/地图版即时查询接口指令8003
                'RequestData' => urlencode($requestData) ,
                'DataType' => '2',
            );
            $datas['DataSign'] = urlencode(base64_encode(md5($requestData.$config['ApiKey'])));
            $url = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
            //以form表单形式提交post请求，post请求体中包含了应用级参数和系统级参数
            $postdata = http_build_query($datas);
            $options = array(
              'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
              )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            $result = json_decode($result,true);
            $data   = [];
            foreach ($result['Traces'] as $key => $val) {
                array_unshift($data, $val);
            }
            return ['status' => 'success', 'message' => '快递物流查询成功', 'data' => $data];
        } else {
            return ['status' => 'error', 'message' => '快递物流查询未配置', 'data' => []];
        }
    }
}